import cv2
import numpy as np
def drawshape(img,points):
    i=0
    while (i<len(points)):
        if (i==len(points)-1):
            x,y=points[i][0]
            x1,y1=points[0][0]
            cv2.line(img,(x,y),(x1,y1),(255,0,0),5)
        else :
            x,y=points[i][0]
            x1,y1=points[i+1][0]
            cv2.line(img,(x,y),(x1,y1),(255,0,0),5)
        i+=1
img=cv2.imread(r"D:\Project\Project37\Project37\tupian\hand.png")
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,dst=cv2.threshold(gray,90,255,cv2.THRESH_BINARY)
#dst=cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,3,0)
cnt,hie=cv2.findContours(dst,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# r=cv2.minAreaRect(cnt[1])
# box=cv2.boxPoints(r)
# box=np.int32(box)
# cv2.drawContours(img,[box],0,(0,0,255),2)
# cv2.imshow('img',img)
# cv2.waitKey(0)
x,y,w,h=cv2.boundingRect(cnt[0])
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0))






















































































print(cnt)
approx=cv2.approxPolyDP(cnt[0],2,True)
drawshape(img,approx)
cv2.imshow('img',img)
cv2.waitKey(0)
# print(approx)
# area=cv2.contourArea(cnt[4])
# len=cv2.arcLength(cnt[0],True)
# print(area)
# print(len)
# cv2.drawContours(img,cnt,-1,(255,0,0),1)
# cv2.imshow('img',img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()